# coding=UTF-8
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from djopping_favs.models import FavouriteItem, FavouriteTop
from djopping_core.models import Product

def favourites(request):
    try:
        user = request.user
    except:
        user = request['user']
    items = []
    ctype = ContentType.objects.get_for_model(Product)
    if user.is_authenticated():
        [items.append(fav_item.object) \
            for fav_item in list(FavouriteItem.objects.filter(
                user=user,
                content_type=ctype).order_by('?')[:5])]
    else:
        [items.append(fav_item.object) \
            for fav_item in \
                list(FavouriteTop.objects.all().order_by('-how_many')[:5])]

    return {'favourites': items}
